package com.pinyoumei.app.activity.userpersonal;


import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.pinyoumei.app.R;
import com.pinyoumei.app.bean.FirstloadNewBean;
import com.pinyoumei.app.view.CircularImageView;

import java.util.ArrayList;
import java.util.List;

public class MyPerformanceAdapter extends RecyclerView.Adapter<MyPerformanceAdapter.BaseViewHolder> {

    private List<MyPerformanceBean.ResultBean.MyPerformanceDto> listBeans = new ArrayList<>();
    private Context context;

    public MyPerformanceAdapter(Context context){
            this.context = context;
            }

    public void replaceAll(List<MyPerformanceBean.ResultBean.MyPerformanceDto> list) {
            listBeans.clear();
            if (list != null && list.size() > 0) {
            listBeans.addAll(list);
            }
            notifyDataSetChanged();
            }

    /**
     * 插入数据使用notifyItemInserted，如果要使用插入动画，必须使用notifyItemInserted
     * 才会有效果。即便不需要使用插入动画，也建议使用notifyItemInserted方式添加数据，
     * 不然容易出现闪动和间距错乱的问题
     * */
    public void addData(int position, List<MyPerformanceBean.ResultBean.MyPerformanceDto> list) {
            listBeans.addAll(position,list);
            notifyItemInserted(position);
            }

    //移除数据使用notifyItemRemoved
    public void removeData(int position) {
            listBeans.remove(position);
            notifyItemRemoved(position);
            }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new OneViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.my_performance_item, parent, false));
            }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder baseViewHolder, int i) {
            baseViewHolder.setData(listBeans.get(i),i);
            if(null != mOnItemClickLitener){
            baseViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickLitener.onItemClick(null,i);
                    }
                });
            }
    }

    @Override
    public int getItemCount() {
            return listBeans != null ? listBeans.size() : 0;
            }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        void setData(MyPerformanceBean.ResultBean.MyPerformanceDto data, int position) {

        }
    }

    private class OneViewHolder extends MyPerformanceAdapter.BaseViewHolder {
        private ImageView mpItemIcon;
        private CircularImageView mpItemImg;
        private TextView mpItemNum,mpItemTitle,mpItemPer;

        public OneViewHolder(View view) {
            super(view);
            mpItemIcon = (ImageView) view.findViewById(R.id.mp_item_icon);
            mpItemImg = (CircularImageView) view.findViewById(R.id.mp_item_img);
            mpItemNum = (TextView) view.findViewById(R.id.mp_item_num);
            mpItemTitle = (TextView) view.findViewById(R.id.mp_item_title);
            mpItemPer = (TextView) view.findViewById(R.id.mp_item_per);
        }

        @Override
        void setData(MyPerformanceBean.ResultBean.MyPerformanceDto item, int position) {
            if(position == 0){
                mpItemIcon.setImageResource(R.mipmap.mpv_per3);
                mpItemIcon.setVisibility(View.VISIBLE);
                mpItemNum.setTextColor(context.getResources().getColor(R.color.color_018d47));
            }else if (position == 1){
                mpItemIcon.setImageResource(R.mipmap.mpv_per4);
                mpItemIcon.setVisibility(View.VISIBLE);
                mpItemNum.setTextColor(context.getResources().getColor(R.color.color_f88128));
            }else {
                mpItemIcon.setVisibility(View.INVISIBLE);
                mpItemNum.setTextColor(context.getResources().getColor(R.color.color_666666));
            }

            Glide.with(context).load(item.getUserHeadImg()).into(mpItemImg);
            mpItemTitle.setText(item.getUserName());
            mpItemPer.setText("直推业绩："+item.getTeamAmt());
            mpItemNum.setText(item.getTop());
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

        private MyPerformanceAdapter.OnItemClickLitener mOnItemClickLitener;

        public void setOnItemClickLitener(MyPerformanceAdapter.OnItemClickLitener mOnItemClickLitener) {
            this.mOnItemClickLitener = mOnItemClickLitener;
        }

}